﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Ezpay.UI.ViewModels
{
    class NavigationHistoryViewModel
    {
        public DelegateCommand PreviousCommand { get; set; }
        public DelegateCommand NextCommand { get; set; }

        private IRegionNavigationJournal _regionManager;

        public NavigationHistoryViewModel(IRegionManager regionManager)
        {
            regionManager.Regions["MainRegion"].NavigationService.Navigated += NavigationService_Navigated;
            _regionManager = regionManager.Regions["MainRegion"].NavigationService.Journal;

            NextCommand = new DelegateCommand(Next, CanNext);
            PreviousCommand = new DelegateCommand(Previous, CanPrevious);
        }

        void NavigationService_Navigated(object sender, RegionNavigationEventArgs e)
        {
            NextCommand.RaiseCanExecuteChanged();
            PreviousCommand.RaiseCanExecuteChanged();
        }

        private bool CanPrevious()
        {
            return _regionManager.CanGoBack;
        }

        private void Previous()
        {
            _regionManager.GoBack();
            PreviousCommand.RaiseCanExecuteChanged();
        }

        private bool CanNext()
        {
            return _regionManager.CanGoForward;
        }

        private void Next()
        {
            _regionManager.GoForward();
            NextCommand.RaiseCanExecuteChanged();
        }
    }
}
